<?php
namespace app\finance\controller;
use app\common\controller\Common;
use think\Request;
class Pay extends Common
{
	protected $model = null;
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'add,del,add_regular'],
    ];

	public function _initialize()
	{
		parent::_initialize();
		$this->model = new \app\finance\model\Pay;
	}
	/**
	* 支出记账
	*/
	public function index()
	{
		$ty = $this->cateData();
		if(Request()->isAjax()){
			$data = Request()->param('');

			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
			$search = $this->keyword_search($search_arr);
			$order = $this->order;
			$where = $this->handleList($data);
			
			$res['list'] = $this->model->listData($where, $order, $page, $row, $search);
			$res['count']['count'] = $this->model->allMoney($where, $order, $search);
			$res['count']['ty'] = $ty;

			$count = $this->model->listCount($where, $order, $search);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $res, $page, $row);
		}
		if(Request()->isGet()){
			$cate = model('cate')->getCate($ty);
			$this->assign('cate',$cate);
			return $this->fetch('');
		}
	}
	/**
	* 新增支出记账
	*/
	public function add()
	{
		if(Request()->isGet()){
			$ty = $this->cateData();
			$cate = model('cate')->getCate($ty);
			$this->assign('cate',$cate);
			$supplier = $this->model->getSupplier();
			$this->assign('supplier',$supplier);
			/*@产品分类*/
			$gcate = model('staff/Category')->where('fid',0)->select();
			$this->assign('gcate',$gcate);
			return $this->fetch('');
		}
		if(Request()->isAjax()){
			$data = Request()->param('');
			$cate_model = model('staff/Category');
			$this->model->addtime = time();
			if(session('admin.admin_type') != 0){
				$data['shop_id'] = session('admin.shop_id');
			}
			if( in_array($data['expend_cate_id'], config('status.pay_config')) ){
				/*@产品分类名*/
				$data['cat_f_name'] = $cate_model->where('category_id',$data['category_id'])->value('category_name');
				/*@产品子分类名*/
				$data['cat_s_name'] = $cate_model->where('category_id',$data['cat_id'])->value('category_name');
			}
			try{
				$this->model->allowField(true)->save($data);
				$this->log_id = $this->model->expend_id;
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	 * 删除
	 */
	public function del()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			if(!isset($data['id']) || $data['id'] == ''){
				$this->error(config('status.ERROR_ONE'));
			}
			$this->log_id = $data['id'];
			try{
				$this->model->where('expend_id',$data['id'])->delete();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	* 支出预算
	*/
	public function budget()
	{
		if(Request()->isGet()){
			$month_money = $this->model->monthMoney();//按月支出
			$this->assign('month_money',$month_money);
			$single_money = $this->model->singleMoney();//单笔支出
			$this->assign('single_money',$single_money);
			$regular_money = $this->model->regularMoney();//定期支出
			$this->assign('regular_money',$regular_money);
			return $this->fetch('');
		}
	}
	/**
	* 新增定期支付
	*/
	public function add_regular()
	{
		if(Request()->isGet()){
			$cate = model('cate')->getCate(2);
			$this->assign('cate',$cate);
			return $this->fetch('');
		}
	}

	/************************************ 私有 **************************************/
	/**
	* 记账分类
	*/
	private function cateData()
	{
		switch(session('admin.admin_type')){
			case 0: return $ty = 0;break;
			case 1:
			case 2:
			return $ty = 1;break;
			default: $this->error('status.ERROR_SIX');break;
		}
	}
	/**
	* 支出记账-列表数据处理
	*/
	protected function handleList($data)
	{
		$where = [];
		/*@登录者*/
		switch(session('admin.admin_type')){
			case 0: $where['shop_id'] = 0;break;//总部
			case 1:
			case 2:
			$where['shop_id'] = session('admin.shop_id');break;
			default: $this->error('status.ERROR_SIX');break;
		}
		/*@支出类型*/
		if(isset($data['expend_cate_id']) && $data['expend_cate_id'] != ''){
			$where['expend_cate_id'] = $data['expend_cate_id'];
		}
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}